<?php
/**
 *2016.8.10 删除文件及其子文夹下文件
 * User: wuz
 * Date: 2016/8/10
 * Time: 22:07
 */

namespace Ycf\Lession\FormAndFile;

class DeleteFile
{
    public function file($dirname)
    {
        $deleteEmptyDir[] = $dirname; //将文件夹存数组中，用于删除文件夹
        $dirHandle        = opendir($dirname);
        while ($file = readdir($dirHandle)) {
            $dirFile = $dirname . "/" . $file;
            if ($file != '.' && $file != '..') {
                if (is_dir($dirFile)) {
                    $this->file($dirFile);
                }
                if (is_file($dirFile)) {
                    unlink($dirFile);
                    echo "成功删除文件：" . $dirFile;
                }
            }
        }
        closedir($dirHandle);
        //删除文件夹
        foreach ($deleteEmptyDir as $emptyDir) {
            rmdir($emptyDir);
        }
    }
}
